import requests
from lxml import etree

from RoxyLogger import RoxyLogger


class BaikeModule:

    def __init__(self):
        """
        百科模块，从百度百科爬取
        """

        self.headers = {
            "user - agent": "Mozilla / 5.0(WindowsNT6.1;WOW64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 63.0.3239.132Safari / 537.36",
        }
        self.l = RoxyLogger()
        # self.url = "https://baike.baidu.com/search/word?fromModule=lemma_search-box&lemmaId=lemma_search-box&word=python"

    def get_request(self, url):
        """
        从百度百科爬取
        :param url: 爬取链接
        :return: 字典，查询的结果
        """

        r = requests.get(url=url, headers=self.headers).text
        tree = etree.HTML(r)

        res = {"main_title": "未找到结果", "sub_title": "", "text": ""}

        try:
            main_title = tree.xpath("/html/body/div[3]/div[1]/div[2]/div/div[1]/dl/dd/span[1]/h1")[0].text
            sub_title = tree.xpath("/html/body/div[3]/div[1]/div[2]/div/div[1]/dl/div")[0].text
            res = {"main_title": main_title, "sub_title": sub_title, "text": r.split("<meta name=\"description\" content=\"")[1].split("\">")[0]}
        except IndexError:
            try:
                main_title = tree.xpath("/html/body/div[3]/div[2]/div/div[1]/dl[1]/dd/span[1]/h1")[0].text
                sub_title = tree.xpath("/html/body/div[3]/div[2]/div/div[1]/dl[1]/div")[0].text
                res = {"main_title": main_title, "sub_title": sub_title,
                        "text": r.split("<meta name=\"description\" content=\"")[1].split("\">")[0]}
            except IndexError:
                main_title = tree.xpath("/html/body/div[3]/div[1]/div/div[1]/div[1]/dl/dd/span[1]/h1")[0].text
                sub_title = tree.xpath("/html/body/div[3]/div[1]/div/div[1]/div[1]/dl/div")[0].text
                res = {"main_title": main_title, "sub_title": sub_title,
                       "text": r.split("<meta name=\"description\" content=\"")[1].split("\">")[0]}
        except Exception as e:
            print("Baike Module Error: ", e)

        return res


if __name__ == "__main__":
    baike_module = BaikeModule()
    print(baike_module.get_request())